package com.cndw.kungfu.dao;

import java.util.List;

import com.cndw.dao.jade.Identity;
import com.cndw.dao.jade.annotation.DAO;
import com.cndw.dao.jade.annotation.SQL;
import com.cndw.dao.jade.annotation.SQLParam;
import com.cndw.kungfu.domain.Union;
import com.cndw.kungfu.domain.UnionAsset;
import com.cndw.kungfu.domain.UnionCashCow;
import com.cndw.kungfu.domain.UnionConfig;
import com.cndw.kungfu.domain.UnionMember;
import com.cndw.kungfu.domain.UnionSkillConfig;

/**
 * 帮派管理持久化接口
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-5-18
 */
@DAO
public interface UnionDao {

	public static final String UNION_TABLE = " kf_union ";
	public static final String UNION_MEMBER_TABLE = " kf_union_member ";
	public static final String UNION_FIELDS = "id,name,level,playerId,playerName,description,notice,validMode,resource,asset,memberCount,skill,addTime,petLv";
	public static final String UNION_LIST_FIELDS = "id,name,level,playerId,playerName,resource,asset,memberCount,addTime";
	public static final String UNION_MEMBER_FIELDS = "unionId,playerId,position,flag,resource,hissource,counts,waterTime,addTime,getTime";
	public static final String UNION_ASSET_TABLE = " kf_union_asset ";
	public static final String UNION_ASSET_FIELDS = "unionId,playerId,playerName,asset,resource,type,eventType,addTime";
	public static final String UNION_CASH_COW_TABLE = " kf_union_cashcow ";
	public static final String UNION_CASH_COW_FIELD = "unionId,growValue,rewardCounts";

	/**
	 * 新增摇钱树
	 * 
	 * @param union
	 * @return
	 */
	@SQL("insert into " + UNION_CASH_COW_TABLE + " (" + UNION_CASH_COW_FIELD + ") values(:c.unionId,:c.growValue,:c.rewardCounts)")
	public void insertUnionCashCow(@SQLParam("c") UnionCashCow cashCow);

	/**
	 * 删除摇钱树
	 * 
	 * @param unionId
	 */
	@SQL("delete from  " + UNION_CASH_COW_TABLE + "where unionId=:unionId")
	public void deleteUnionCashCow(@SQLParam("unionId") int unionId);

	/**
	 * 获取摇钱树
	 * 
	 * @param union
	 */
	@SQL("select " + UNION_CASH_COW_FIELD + " from " + UNION_CASH_COW_TABLE + " where unionId = :unionId")
	public UnionCashCow getUnionCashCow(@SQLParam("unionId") int unionId);

	/**
	 * 修改剩余奖励份数
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_CASH_COW_TABLE + " set rewardCounts=:c.rewardCounts where unionId = :c.unionId")
	public void updateUnionCashCowReward(@SQLParam("c") UnionCashCow cashCow);

	/**
	 * 修改成长值
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_CASH_COW_TABLE + " set growValue=:c.growValue where unionId = :c.unionId")
	public void updateUnionCashGrowValue(@SQLParam("c") UnionCashCow cashCow);

	/**
	 * 获取帮派系统配置信息
	 * 
	 * @return
	 */
	@SQL("select level,name,levelResource,levelAsset,memberLimit,jobIds,assetLimit,capacity from kfsys_union_config order by level")
	public List<UnionConfig> getAllUnionConfig();

	/**
	 * 获取帮派技能配置信息
	 * 
	 * @return
	 */
	@SQL("select id,type,name,level,requiedLevel,effect,resource,asset,useResource,useAsset,icon,description from kfsys_union_skill order by type,level")
	public List<UnionSkillConfig> getAllUnionSkillConfig();

	/**
	 * 获取所有帮派
	 * 
	 * @return
	 */
	@SQL("select id from " + UNION_TABLE + " where addTime < :1")
	public List<Integer> getMaintainUnionIds(int time);

	/**
	 * 获取帮派列表
	 * 
	 * @param offset
	 * @param limit
	 * @param search
	 * @param order
	 * @return
	 */
	@SQL("select " + UNION_LIST_FIELDS + " from " + UNION_TABLE + " #if(:search){where name like :search} order by level desc,addTime limit :offset,:limit ")
	public List<Union> getUnionList(@SQLParam("offset") int offset, @SQLParam("limit") int limit, @SQLParam("search") String search);

	/**
	 * 获取帮派数量
	 * 
	 * @param search
	 * @return
	 */
	@SQL("select count(*) from " + UNION_TABLE + " #if(:search){where name like :search}")
	public int getUnionListCounnt(@SQLParam("search") String search);

	/**
	 * 删除帮派
	 * 
	 * @param unionId
	 */
	@SQL("delete from  " + UNION_TABLE + "where id=:unionId")
	public void deleteUnion(@SQLParam("unionId") int unionId);

	/**
	 * 帮主退位
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set playerId=:u.playerId,playerName=:u.playerName where id=:u.id")
	public void abdicate(@SQLParam("u") Union union);

	@SQL("update "
			+ UNION_TABLE
			+ " set level=:u.level,playerId=:u.playerId,playerName=:u.playerName,description=:u.description,notice=:u.notice,validMode=:u.validMode,resource=:u.resource,asset=:u.asset,memberCount=:u.memberCount,resource=:u.resource,addTime=:u.addTime,petLv=:u.petLv where id=:u.id")
	public void updateUnion(@SQLParam("u") Union union);

	/**
	 * 修改帮派财富
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set asset=:u.asset where id=:u.id")
	public void updateUnionAsset(@SQLParam("u") Union union);
	
	/**
	 * 升级帮派神兽
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set petLv=:u.petLv,resource=:u.resource,asset=:u.asset where id=:u.id")
	public void updatePetLv(@SQLParam("u") Union union);
	/**
	 * 修改帮派资源财富
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set resource=:u.resource,asset=:u.asset where id=:u.id")
	public void updateUnionResAsset(@SQLParam("u") Union union);

	/**
	 * 修改帮派贡献点
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set resource=:u.resource where id=:u.id")
	public void updateUnionResource(@SQLParam("u") Union union);

	/**
	 * 升级帮派等级
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set level=:u.level,resource=:u.resource,asset=:u.asset where id=:u.id")
	public void upgradeUnionLevel(@SQLParam("u") Union union);

	/**
	 * 升级帮派技能等级
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set skill=:u.skill,resource=:u.resource,asset=:u.asset where id=:u.id")
	public void upgradeUnionSkillLevel(@SQLParam("u") Union union);

	@SQL("update " + UNION_TABLE + " set resource=:u.resource,asset=:u.asset where id=:u.id")
	public void upgradeTechLevel(@SQLParam("u") Union union);

	@SQL("update " + UNION_TABLE + " set resource=:u.resource,asset=:u.asset where id=:u.id")
	public void addResource(@SQLParam("u") Union union);

	/**
	 * 发布公告
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set notice=:u.notice where id=:u.id")
	public void publishNotice(@SQLParam("u") Union union);

	/**
	 * 设置帮派
	 * 
	 * @param union
	 */
	@SQL("update " + UNION_TABLE + " set validMode=:u.validMode where id=:u.id")
	public void settingUnion(@SQLParam("u") Union union);

	/**
	 * 增加帮派成员人数
	 * 
	 * @param count
	 * @param id
	 */
	@SQL("update " + UNION_TABLE + " set memberCount=memberCount+:count where id=:id")
	public void increaseUnionMemberCount(@SQLParam("count") int count, @SQLParam("id") int id);

	/**
	 * 减少帮派成员人数
	 * 
	 * @param count
	 * @param id
	 */
	@SQL("update " + UNION_TABLE + " set memberCount=memberCount-:count where id=:id")
	public void decreaseMemberCount(@SQLParam("count") int count, @SQLParam("id") int id);

	/**
	 * 新增帮派
	 * 
	 * @param union
	 * @return
	 */
	@SQL("insert into "
			+ UNION_TABLE
			+ " ("
			+ UNION_FIELDS
			+ ") values(:u.id,:u.name,:u.level,:u.playerId,:u.playerName,:u.description,:u.notice,:u.validMode,:u.resource,:u.asset,:u.memberCount,:u.skill,:u.addTime,:u.petLv)")
	public Identity insertUnion(@SQLParam("u") Union union);

	/**
	 * 检查帮派名字是否存在
	 * 
	 * @param name
	 * @return
	 */
	@SQL("select id from " + UNION_TABLE + " where name = :name")
	public Union checkUnionExist(@SQLParam("name") String name);

	/**
	 * 获取帮派
	 * 
	 * @param id
	 * @return
	 */
	@SQL("select " + UNION_FIELDS + " from " + UNION_TABLE + " where id = :id")
	public Union getUnionById(@SQLParam("id") int id);

	/**
	 * 新增帮派成员
	 * 
	 * @param member
	 * @return
	 */
	@SQL("insert into " + UNION_MEMBER_TABLE + " (" + UNION_MEMBER_FIELDS
			+ ") values(:m.unionId,:m.playerId,:m.position,:m.flag,:m.resource,:m.hissource,:m.counts,:m.waterTime,:m.addTime,:m.getTime)")
	public int insertUnionMember(@SQLParam("m") UnionMember member);

	/**
	 * 删除帮派所有成员
	 * 
	 * @param unionId
	 */
	@SQL("delete from " + UNION_MEMBER_TABLE + "where unionId=:unionId")
	public void deleteAllUnionMember(@SQLParam("unionId") int unionId);

	/**
	 * 删除帮派成员
	 * 
	 * @param member
	 */
	@SQL("delete from " + UNION_MEMBER_TABLE + " where unionId=:m.unionId and playerId=:m.playerId")
	public void deleteUnionMember(@SQLParam("m") UnionMember member);

	/**
	 * 设置成员信息
	 * 
	 * @param member
	 */
	@SQL("update " + UNION_MEMBER_TABLE + " set position=:m.position,flag=:m.flag where unionId=:m.unionId and playerId=:m.playerId")
	public void settingMember(@SQLParam("m") UnionMember member);

	/**
	 * 修改成员配置
	 * 
	 * @param member
	 */
	@SQL("update " + UNION_MEMBER_TABLE + " set position=:m.position where unionId=:m.unionId and playerId=:m.playerId")
	public void abdicateMember(@SQLParam("m") UnionMember member);

	/**
	 * 修改成员贡献点
	 * 
	 * @param member
	 */
	@SQL("update " + UNION_MEMBER_TABLE + " set resource=:m.resource,hissource=:m.hissource where unionId=:m.unionId and playerId=:m.playerId")
	public void updateMemberResource(@SQLParam("m") UnionMember member);

	/**
	 * 浇水
	 * 
	 * @param member
	 */
	@SQL("update " + UNION_MEMBER_TABLE + " set waterTime = :m.waterTime where unionId=:m.unionId and playerId=:m.playerId")
	public void watering(@SQLParam("m") UnionMember member);

	/**
	 * 领取奖励
	 * 
	 * @param member
	 */
	@SQL("update " + UNION_MEMBER_TABLE + " set counts  = :m.counts,getTime = :m.getTime where unionId=:m.unionId and playerId=:m.playerId")
	public void updateCashCowReward(@SQLParam("m") UnionMember member);

	/**
	 * 获取帮派成员
	 * 
	 * @param id
	 * @return
	 */
	@SQL("select " + UNION_MEMBER_FIELDS + " from " + UNION_MEMBER_TABLE + " where unionId = :unionId")
	public List<UnionMember> getUnionMemberByUnionId(@SQLParam("unionId") int id);

	/**
	 * 新增帮派资金记录
	 * 
	 * @param contribution
	 * @return
	 */
	@SQL("insert into " + UNION_ASSET_TABLE + " (" + UNION_ASSET_FIELDS + ") values(:c.unionId,:c.playerId,:c.playerName,:c.asset,:c.resource,:c.type,:c.eventType,:c.addTime)")
	public void addUnionAsset(@SQLParam("c") UnionAsset unionAsset);

	/**
	 * 获取资金增加列表
	 * 
	 * @param unionId
	 * @param offset
	 * @param limit
	 * @param order
	 * @return
	 */
	@SQL("select " + UNION_ASSET_FIELDS + " from " + UNION_ASSET_TABLE + " where unionId=:unionId and type=1 order by addTime desc limit 25 ")
	public List<UnionAsset> getAddUnionAssets(@SQLParam("unionId") int unionId);

	/**
	 * 获取资金消耗列表
	 * 
	 * @param unionId
	 * @param offset
	 * @param limit
	 * @param order
	 * @return
	 */
	@SQL("select " + UNION_ASSET_FIELDS + " from " + UNION_ASSET_TABLE + " where unionId=:unionId and type=2 order by addTime desc limit 25 ")
	public List<UnionAsset> getExpendUnionAssets(@SQLParam("unionId") int unionId);

	/**
	 * 获取指定等级的帮派数量
	 * 
	 * @param level
	 * @return
	 */
	@SQL("select count(*) from " + UNION_TABLE + " where level >= :level")
	public int getUnionCountByLevel(@SQLParam("level") int level);

	@SQL("select max(id) from " + UNION_TABLE + " ")
	public int getMaxUnionId();
	
	@SQL("select " + UNION_FIELDS + " from " + UNION_TABLE + " where petLv > 0 order by petLv desc limit 8 ")
	public List<Union> getByUnionPetOrder();

}
